package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.ZhenliaoMapper;
import cn.tedu.hospitalqd.pojo.dto.ZhenliaoAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.ZhenliaoUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Zhenliao;
import cn.tedu.hospitalqd.pojo.vo.ZhenliaoListItemVO;
import cn.tedu.hospitalqd.pojo.vo.ZhenliaoStandarVO;
import cn.tedu.hospitalqd.service.ZhenliaoService;
import cn.tedu.hospitalqd.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ZhenliaoServiceImpl implements ZhenliaoService {

    @Autowired
    private ZhenliaoMapper zhenliaoMapper;

    @Override
    public void addNew(ZhenliaoAddNewDTO zhenliaoAddNewDTO) {
        // 从参数对象中获取相册名称
        String name = zhenliaoAddNewDTO.getDName();
        // 调用albumMapper的int countByName(String name)根据名称执行统计
        int countByName = zhenliaoMapper.countByName(name);
        // 判断统计结果是否大于0
        if (countByName > 0) {
            // 是：抛出异常
            String message = "添加相册失败，相册名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建Album对象
        Zhenliao zhenliao = new Zhenliao();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到Album对象中
        BeanUtils.copyProperties(zhenliaoAddNewDTO, zhenliao);
        // 调用albumMapper的int insert(Album album)方法将相册数据插入到数据库

        int rows = zhenliaoMapper.insert(zhenliao);
        if (rows != 1) {
            String message = "添加相册失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        ZhenliaoStandarVO queryResult = zhenliaoMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除相册失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = zhenliaoMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除相册失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, ZhenliaoUpdateDTO zhenliaoUpdateDTO) {
        ZhenliaoStandarVO queryResult = zhenliaoMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改相册详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = zhenliaoUpdateDTO.getDName();
        int count = zhenliaoMapper.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改相册详情失败，相册名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Zhenliao zhenliao = new Zhenliao();
        BeanUtils.copyProperties(zhenliaoUpdateDTO, zhenliao);
        zhenliao.setId(id);
        int rows = zhenliaoMapper.update(zhenliao);
        if (rows != 1) {
            String message = "修改相册详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ZhenliaoStandarVO getStandardById(Long id) {
        ZhenliaoStandarVO queryResult = zhenliaoMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取相册详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public List<ZhenliaoListItemVO> list() {
        List<ZhenliaoListItemVO> list = zhenliaoMapper.list();
        return list;
    }

}
